<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我的购物车</title>
    <script src="js/jquery-3.3.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jquery.cookie.js"></script>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
    <link rel="stylesheet" type="text/css" href="css/mycar.css"/>
</head>
<body>
    <div class="container" id="returnindex">
        <div class="row">
            <div class="col-xs-9 col-sm-9 col-md-9 col-lg-9"></div>
            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1" id="returnshop"></div>
            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1" id="ordermanage"></div>
            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"><a href="index.html">返回首页</a></div>
        </div>
    </div>
    <div class="container" id="pagetitle">
        <div class="row">
            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"></div>
            <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3"><p id="titlep"><span>乐享生活</span>&nbsp;我的购物车</p></div>
            <div class="col-xs-5 col-sm-5 col-md-5 col-lg-5"></div>
            <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2"></div>
            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"></div>
        </div>
    </div>
    <div class="container" id="carbody">
    </div>

    <script type="text/javascript">

        var userId = $.cookie("userId");

        $.post("che/getCar",{userId:userId},function (data) {
            if (data == "null"){
                alert("亲，您的购物车中没有商品，请添加后再来！");
                location.href = "index.html";
            } else {
                data = JSON.parse(data);

                var address = '';

                for (var i = 0; i < data.addresses.length; i++) {
                    address += '<div><input type="radio" name="address" value="'+ data.addresses[i].id +'">&nbsp;&nbsp;'+ data.addresses[i].receiveName +'&nbsp;&nbsp;'+ data.addresses[i].telephone +'&nbsp;&nbsp;'+ data.addresses[i].province +'&nbsp;&nbsp;'+ data.addresses[i].city +'&nbsp;&nbsp;'+ data.addresses[i].details +'</div>';
                }

                var ht = '<div class="row">\n' +
                    '            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"></div>\n' +
                    '            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1" id="allt">全部商品</div>\n' +
                    '        </div>\n' +
                    '        <div class="row carlisttitle">\n' +
                    '            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"></div>\n' +
                    '            <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6"><input type="checkbox" id="checkall"><label>全选&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;商品信息</label></div>\n' +
                    '            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1">单价</div>\n' +
                    '            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1">数量</div>\n' +
                    '            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1">金额</div>\n' +
                    '            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1">操作</div>\n' +
                    '        </div>';

                for (var i = 0; i < data.cars.length; i++) {
                    ht += ' <div class="row carlist">\n' +
                        '        <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"></div>\n' +
                        '        <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6 listleft"><input type="checkbox" id="'+ data.cars[i].goods.id +'_'+ data.cars[i].goods.shopid +'_'+ data.cars[i].number +'" name="goodscheck" value="'+ parseInt(data.cars[i].number)*parseInt(data.cars[i].goods.price) +'">&nbsp;&nbsp;<img src="/FVOnline/goods/getImg?goodsId='+ data.cars[i].goods.id +'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+ data.cars[i].goods.goodsname +'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+ data.cars[i].goods.details +'</div>\n' +
                        '        <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 listcenter">￥'+ data.cars[i].goods.price +'</div>\n' +
                        '        <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 listcenter">'+ data.cars[i].number +'</div>\n' +
                        '        <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 listcenter">￥'+ parseInt(data.cars[i].number)*parseInt(data.cars[i].goods.price) +'</div>\n' +
                        '        <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 listright"><a>删除</a></div>\n' +
                        ' </div>';
                 }


                 ht += '<div class="row addressrow">\n' +
                     '            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"></div>\n' +
                     '            <div class="col-xs-10 col-sm-10 col-md-10 col-lg-10">\n' +
                     '                <div>请选择收货地址:</div>\n' + address +
                     '                <div id="submitorder">合计：￥<span id="carsum">0</span>（不含运费）<button id="submit">前往结算</button></div>\n' +
                     '            </div>\n' +
                     '        </div>';

                $("#carbody").html(ht);

                $("#checkall").click(function () {
                    if ($("#checkall").prop("checked") == true){
                        $(":checkbox[name = 'goodscheck']").prop("checked",true);
                        var sum = 0;
                        $.each($(":checkbox[name = 'goodscheck']"),function () {
                            sum += parseInt($(this).val());
                        });
                        $("#carsum").html(sum);
                    }else {
                        $(":checkbox[name = 'goodscheck']").prop("checked",false);
                        $("#carsum").html(0);
                    }
                });

                $(":checkbox[name = 'goodscheck']").click(function () {
                    if ($(this).prop("checked") == true){
                        var sum = 0;

                        sum = parseInt($("#carsum").html()) +  parseInt($(this).val());

                        $("#carsum").html(sum);
                    }else {
                        var sum = 0;

                        sum = parseInt($("#carsum").html()) -  parseInt($(this).val());

                        $("#carsum").html(sum);
                    }
                });

                $("#submit").click(function () {
                    if ($('input[name="address"]:checked').val() == null){
                        alert("请选择收货地址！");
                    }else if ($("#carsum").html() == 0) {
                       alert("您未选择任何商品！");
                    }else {

                        var addressid = $('input[name="address"]:checked').val();

                        $.ajaxSettings.async = false;

                        $.each($(":checkbox[name = 'goodscheck']"),function () {

                            if ($(this).prop("checked") == true){
                                var goodsid = this.id.split("_")[0];
                                var shopid = this.id.split("_")[1];
                                var buynum = this.id.split("_")[2];
                                var pricesum = $(this).val();
                                $.post("order/createOrder",{userId:userId,goodsid:goodsid,shopid:shopid,addressid:addressid,buynum:buynum,pricesum:pricesum,remarks:""},function (data3) {
                                });
                                $.post("che/removegoods",{userId:userId,goodsid:goodsid,num:buynum},function (result) {
                                });
                            }


                        });

                        $.ajaxSettings.async = true;
                        alert("购物车结算成功！");
                        location.href ="mycar.html";
                    }
                });

            }
        })

    </script>
</body>
</html>